/*
 * Copyright (c) 2009 by ZDO Corporation.
 * Author: A. Onur Cinar
 * 
 * http://code.google.com/p/zdo-struts2
 * http://www.zdo.com
 * 
 * This file is part of ZDO Struts 2 Applications.
 *
 * ZDO-Struts2 is free software: you can redistribute it and/or
 * modify it under the terms of  the GNU General Public License
 * as published by the Free Software Foundation, either version
 * 3 of the License, or any later version.
 *
 * ZDO-Struts2 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */

package com.zdo.actions;

import javax.persistence.EntityManager;

import com.opensymphony.xwork2.ActionSupport;
import com.zdo.persistence.PersistenceAware;
import com.zdo.security.SecurityAware;
import com.zdo.security.SecurityContext;

/**
 * Default action. Provides default implementations for 
 * {@link PersistenceAware} and {@link SecurityAware}.
 */
public class DefaultAction extends ActionSupport implements PersistenceAware,
		SecurityAware
{
	private static final long serialVersionUID = -1931259747045386212L;
	
	private EntityManager entityManager;
	private SecurityContext securityContext;

	/**
	 * Gets the {@link EntityManager} instance.
	 * 
	 * @return entity manager.
	 */
	public EntityManager getEntityManager ()
	{
		return entityManager;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void setEntityManager (EntityManager entityManager)
	{
		this.entityManager = entityManager;
	}

	/**
	 * Gets the {@link SecurityContext} instance.
	 * 
	 * @return security context.
	 */
	public SecurityContext getSecurityContext ()
	{
		return securityContext;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void setSecurityContext (SecurityContext securityContext)
	{
		this.securityContext = securityContext;
	}
	
	/**
	 * Begins transaction.
	 */
	public void begin ()
	{
		getEntityManager().getTransaction().begin();
	}
	
	/**
	 * Commits transaction.
	 */
	public void commit ()
	{
		getEntityManager().getTransaction().commit();
	}
}
